package com.sgl.myview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.sgl.myview.R;

/**
 * Created by XH on 2016/11/29.
 */
public class CustomTitleView extends View {
    private String mTitleText;//文本
    private int mTitleTextColor;//文本颜色
    private int mTitleTextSize;//文本字体
    private Boolean mResult = true;//是否点击了


    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    private Paint mPaint;


    public CustomTitleView(Context context) {
        this(context,null);
    }

    public CustomTitleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取我们所自定义的样式
        TypedArray mTypedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView,defStyleAttr,0);
        for(int i = 0;i < mTypedArray.getIndexCount(); i++){
            int attr = mTypedArray.getIndex(i);
            switch (attr){
                case R.styleable.CustomTitleView_myTitleText:
                    mTitleText = mTypedArray.getString(attr);
                    break;
                case R.styleable.CustomTitleView_myTitleTextColor:
                    // 默认颜色设置为黑色
                    mTitleTextColor = mTypedArray.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomTitleView_myTitleTextSize:
                    // 默认设置为16sp，TypeValue也可以把sp转化为px
                    mTitleTextSize = mTypedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomTitleView_myTitleTextChooseResult:
                    mResult = mTypedArray.getBoolean(attr,true);
                    mTitleText = mResult ? "yes" : "no";
                    break;
            }
        }

        mTypedArray.recycle();

        /**
         * 获得绘制文本的宽和高
         */
        mPaint = new Paint();
        mPaint.setTextSize(mTitleTextSize);
        // mPaint.setColor(mTitleTextColor);
        mBound = new Rect();
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
    }

    /**
     *系统帮我们测量的高度和宽度都是MATCH_PARNET，当我们设置明确的宽度和高度时，系统帮我们测量的结果就是我们设置的结果，
     * 当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。
     * 所以，当设置了WRAP_CONTENT时，我们需要自己进行测量，即重写onMesure方法”：
     * 重写之前先了解MeasureSpec的specMode,一共三种类型：
     * EXACTLY：一般是设置了明确的值或者是MATCH_PARENT
     * AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT
     * UNSPECIFIED：表示子布局想要多大就多大，很少使用
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height ;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textWidth = mBound.width();
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textHeight = mBound.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //第一步画背景
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

        //第二部画文字
        mPaint.setColor(mTitleTextColor);
        //canvas.drawText(mTitleText,0,0,mPaint);
        mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);
        Log.e("qwer","mTitleText : " + mTitleText);
        Log.e("qwer","getWidth() : " + getWidth());
        Log.e("qwer","mBound.width() : " + mBound.width());
        Log.e("qwer","getHeight() : " + getHeight());
        Log.e("qwer","mBound.height() : " + mBound.height());
        canvas.drawText(mTitleText,getWidth() / 2 - mBound.width() /2,getHeight() / 2 + mBound.height() / 2,mPaint);
    }

    public void setChooseListener(final ChooseListener mChooseListener) {
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mResult){
                    mChooseListener.chooseN();
                    mTitleText = "no";
                }else {
                    mChooseListener.chooseY();
                    mTitleText = "tes";
                }
                mResult = !mResult;
                postInvalidate();
            }
        });
    }

    public interface ChooseListener{
        void chooseY();
        void chooseN();
    }
}
